package com.aizhixin.lab.common.core;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

public class PageUtil {

    public static PageRequest createPageRequest(final Integer pageNumber, final Integer pageSize) {
        if (null == pageNumber || null == pageSize || 0 >= pageSize) {
            return new PageRequest(0, 20);
        }
        if (pageNumber - 1 <= 0) {
            return new PageRequest(0, pageSize);
        }
        return new PageRequest(pageNumber - 1, pageSize);
    }

    public static PageRequest createRequestAndSortType(final Integer pageNumber, final Integer pageSize, String sortType, String cm) {
        Sort sort = null;
        if ("asc".equals(sortType.toLowerCase())) {
            sort = new Sort(Sort.Direction.ASC, cm);
        } else {
            sort = new Sort(Sort.Direction.DESC, cm);
        }
        if (null == pageNumber || null == pageSize || 0 >= pageSize) {
            return new PageRequest(0, 10, sort);
        }
        if (pageNumber - 1 <= 0) {
            return new PageRequest(0, pageSize, sort);
        }
        return new PageRequest(pageNumber - 1, pageSize, sort);
    }

    public static PageRequest createRequestAndSort(final Integer pageNumber, final Integer pageSize, Sort sort) {
        if (null == pageNumber || null == pageSize || 0 >= pageSize) {
            return new PageRequest(0, 10, sort);
        }
        if (pageNumber - 1 <= 0) {
            return new PageRequest(0, pageSize, sort);
        }
        return new PageRequest(pageNumber - 1, pageSize, sort);
    }
}
